package com.gitee.huanminabc.dynamicdb.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.boot.jdbc.DataSourceBuilder;

import javax.sql.DataSource;
import java.io.Serializable;
import java.util.Date;

/**
 * @author huanmin
 * @date 2024/11/22
 */
@Data
@TableName("t_datasource")
public class DataSourceEneity  implements Serializable {
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    @TableField("`key`")
    private String key; //绑定的key,用于数据源的切换
    @TableField("url")
    private String url; //数据库连接地址
    @TableField("username")
    private String username;//数据库用户名
    @TableField("password")
    private String password;//数据库密码
    @TableField("driver_class_name")
    private String driverClassName;//数据库驱动
    @TableField("pool")
    private String pool; //连接池,如:com.zaxxer.hikari.HikariDataSource
    @TableField("type")
    private String type; //数据库类型,如:mysql
    @TableField("state")
    private Integer state;//是否可用: 1可用 ,2不可用
    @TableField("create_time")
    private Date createTime;//创建时间
    @TableField("update_time")
    private Date updateTime;//更新时间
    @TableField("update_by")
    private String updateBy;//更新人
    @TableField("create_by")
    private String createBy;//创建人
    @TableField("remark")
    private String remark;//备注



    public  DataSource getDataSource() {
        DataSourceBuilder<?> builder =  DataSourceBuilder.create();
        try {
            builder.driverClassName(driverClassName);
            builder.username(username);
            builder.password(password);
            builder.url(url);
            //将pool转换为class
            Class aClass = Class.forName(pool);
            builder.type(aClass);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return  builder.build();
    }

}
